/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pdlv2;
import structures.*;
import graph.*;

/**
 *
 * @author Gonzalo
 */
public class Performance {
    private Disk disk;
    private Disk disk_aux;
    private Edges edges;
    
    private long speed=0;
    private long speed_intervalo=0;
    private long speed_intervalo_k=0;


    public Performance(){
        disk=new Disk("D:/Tesis/GDB/friend_.gdb",8);
        disk_aux=new Disk("D:/Tesis/GDB/friend_.gdb.aux",128);
        edges=new Edges(disk,disk_aux);
        //performanceDisk();
        //performanceDiskRandom();
        //performanceEdges();
        //performanceMap();
        //performanceSet();
        performanceNodes();
    }

    public void performanceDisk(){
        for(int i=0;i<5;i++){
            speed("");
            for(disk.gotoLine(1);disk.notNullLine();disk.nextLine()){
                disk.getFirstValue();
                while(!disk.isNextLastValue())
                    disk.getNextValue();
                disk.getLastValue();
            }
            for(disk_aux.gotoLine(1);disk_aux.notNullLine();disk_aux.nextLine()){
                disk_aux.getFirstValue();
                while(!disk_aux.isNextLastValue())
                    disk_aux.getNextValue();
                disk_aux.getLastValue();
            }
            speed("Iterador Disk: ");
        }
    }

    public void performanceDiskRandom(){
        long p=0;
            speed("");
            for(disk.gotoLine(1);disk.notNullLine();disk.nextLine()){
                disk.getFirstValue();
                while(!disk.isNextLastValue())
                    disk.getNextValue();
                p=disk.getLastValue();
                while(p!=0){
                    disk_aux.gotoLine(p);
                    disk_aux.getFirstValue();
                    while(!disk_aux.isNextLastValue())
                        disk_aux.getNextValue();
                    p=disk_aux.getLastValue();
                }
            }
            speed("Iterador DiskRandom: ");
    }

    public void performanceEdges(){
        for(int i=0;i<10;i++){
            speed("");
            for(edges.initKey();edges.notNullKey();edges.nextKey()){//arcos.gdb
                for(edges.initValue();edges.notNullValue();edges.nextValue()){
                    edges.getValue();
                }
            }
            for(edges.initLazy();edges.notNullLazy();edges.nextLazy()){ //arcos.gdb.aux
                for(edges.initLazyValue();edges.notNullValue();edges.nextLazyValue()){
                    edges.getValue();
                }
            }
            speed("Iterador Edges: ");
        }
    }

    public void performanceMap(){
        Map lazy_punteros=new Map();
        for(int i=0;i<10;i++){
            speed("");
            for(long k=0;k<10000000;k++)
                lazy_punteros.put(k, 2*k);
            lazy_punteros.init();
            while(!lazy_punteros.isEmpty()){
                lazy_punteros.pollFirstKey();
            }

            speed("Insert/Remove Map: ");
        }
    }

    public void performanceSet(){
        for(int i=0;i<10;i++){
            Set set=new Set(1000000);
            speed("");
            for(long k=1;k<=1000000;k++)
                set.add(k);
            for(set.init();set.notNull();set.next())
                set.getActual();
            speed("Insert/Iterate Set: ");
        }
    }

    public void performanceNodes(){
        for(int i=0;i<10;i++){
            Nodes nodes1=new Nodes();
            Nodes nodes2=new Nodes();
            speed("");
            for(long k=1;k<=1000000;k++){
                nodes1.add(2*k%1000000+1);
                nodes2.add((2*k+1)%1000000+1);
            }
            nodes1.diferencia(nodes2);
            nodes1.union(nodes2);
            nodes1.interseccion(nodes2);
            speed("Diferencia/Union/Interseccion Nodes: ");
        }
    }

    public void performanceMax(){
        for(int i=0;i<10;i++){
            long index=0;
            for(long k=1;k<=1000000;k++){
                if(index==0)
                    index=k;
            }
            speed("Max Nodes: ");
        }
    }

    public void debug(String s){System.out.println(s);}
    public void debug(int i){System.out.println(""+i);}
    public void debug(long i){System.out.println(""+i);}
    public void debug(Exception ex){System.out.println(ex.toString());}
    public void speed(String s){if(speed==0) debug("Iniciando Speed"); else if(!s.isEmpty()) debug(s+" ("+(System.currentTimeMillis()-speed)+" ms)");speed=System.currentTimeMillis();}
    public void speed(){speed("Speed");}
    public void speedStart(){speed_intervalo_k=0;}
    public void speedFrom(){speed_intervalo_k=System.currentTimeMillis();}
    public void speedTo(){speed_intervalo=speed_intervalo+System.currentTimeMillis()-speed_intervalo_k;}
    public void speedCalc(String s){debug(s+": "+speed_intervalo+" ms");}


}
